package com.study.leetcode;
//https://leetcode-cn.com/problems/maximum-product-subarray/
public class Solution_152 {
    public int maxProduct(int[] nums) {
        int length = nums.length;
        int temp_left = 1;
        int temp_right = 1;
        int max = nums[0];
        for (int i = 0; i < length; i++) {
            temp_right *= nums[i];
            if (temp_right > max) max = temp_right;
            if (nums[i] == 0) temp_right = 1;

            temp_left *= nums[length - 1 - i];
            if (temp_left > max) max = temp_left;
            if (nums[length - 1 - i] == 0) temp_left = 1;
        }

        return max;
    }

    public static void main(String[] args) {
        Solution_152 solution152 = new Solution_152();
        int[] nums1 = {2, 3, -2, 4};
        solution152.maxProduct(nums1);
        int[] nums2 = {5, 3, -2, 1, -1, 9, -31};
        solution152.maxProduct(nums2);
        int[] nums3 = {-1,-1,0};
        solution152.maxProduct(nums3);
    }
}
